package org.happy.tester.hitest.support.listener;

import org.happy.tester.common.http.CatcherClient;
import org.happy.tester.common.util.InternetUtils;
import org.happy.tester.hitest.enums.PointcutTypeEnum;
import org.happy.tester.hitest.support.annotation.Pointcut;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;

import static org.happy.tester.common.constant.CommonConstant.FILE_PATH_JOINER;
import static org.happy.tester.common.constant.CommonConstant.POINTCUT_JOINER;
import static org.happy.tester.hitest.support.listener.HiTestMethodInterceptor.RESULT_LOCATION;

/**
 * @Classname HiTestListenerAdapter
 * @Description 在每一个 collect 模式测试方法执行完毕之后，收集 pointcut 方法返回数据
 * @Date 2020/6/30 6:47 下午
 * @Created by jc.bai
 */
public class HiTestInvokedMethodListener implements IInvokedMethodListener {

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        Method hiMethod = method.getTestMethod().getConstructorOrMethod().getMethod();
        if (hiMethod.isAnnotationPresent(Pointcut.class)) {
            Pointcut pointcut = hiMethod.getAnnotation(Pointcut.class);
            if (pointcut.type() != PointcutTypeEnum.COLLECT) {
                return;
            }
            CatcherClient catcherClient = new CatcherClient();
            String paramTypes = null;
            String[] paramsTypeArray = pointcut.paramsTypeArray().length == 1 && pointcut.paramsTypeArray()[0].equals("") ? null : pointcut.paramsTypeArray();
            if (paramsTypeArray != null) {
                paramTypes = Arrays.toString(paramsTypeArray);
                paramTypes = paramTypes.substring(1, paramTypes.length() - 1);
            }
            FileWriter fileWriter = null;
            BufferedWriter bufferedWriter = null;
            try {
                String point = POINTCUT_JOINER.join(InternetUtils.getOutSideIp(), pointcut.className(), pointcut.methodName(), paramTypes, pointcut.appIp());
                System.out.println(point);
                String result = catcherClient.fetchCollectResult(point);
                if (result != null) {
                    String packagePath = pointcut.className().replaceAll("\\.", File.separator);
                    String physicalPath = System.getProperty("user.dir");
                    String folderPath = FILE_PATH_JOINER.join(physicalPath, RESULT_LOCATION, packagePath);
                    File folder = new File(folderPath);
                    folder.mkdirs();
                    String filePath = FILE_PATH_JOINER.join(folderPath, pointcut.methodName().concat(".json"));
                    File file = new File(filePath);
                    file.createNewFile();
                    file.setWritable(true);
                    fileWriter = new FileWriter(file);
                    bufferedWriter = new BufferedWriter(fileWriter);
                    bufferedWriter.write(result);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bufferedWriter != null) {
                        bufferedWriter.flush();
                        bufferedWriter.close();
                    }
                    if (fileWriter != null) {
                        fileWriter.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
